package com.ruoyi.post.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.post.domain.CanDo;
import com.ruoyi.post.domain.JobInformation;
import com.ruoyi.post.service.IJobInformationService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 岗位信息Controller
 *
 * @author ruoyi
 * @date 2024-06-09
 */
@RestController
@RequestMapping("/post/experiment")
public class JobController extends BaseController
{
    @Autowired
    private IJobInformationService jobInformationService;

    /**
     * 查询岗位信息列表
     */
    //@preAuthorize("@ss.hasPermi('system:information:list')")
    @GetMapping("/list")
    public TableDataInfo list(JobInformation jobInformation)
    {
        startPage();
        List<JobInformation> list = jobInformationService.selectJobInformationList(jobInformation);
        return getDataTable(list);
    }

    /**
     * 导出岗位信息列表
     */
    //@preAuthorize("@ss.hasPermi('system:information:export')")
    @Log(title = "岗位信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JobInformation jobInformation)
    {
        List<JobInformation> list = jobInformationService.selectJobInformationList(jobInformation);
        ExcelUtil<JobInformation> util = new ExcelUtil<JobInformation>(JobInformation.class);
        util.exportExcel(response, list, "岗位信息数据");
    }

    /**
     * 获取岗位信息详细信息
     */
    //@preAuthorize("@ss.hasPermi('system:information:query')")
    @GetMapping(value = "/{jobId}")
    public AjaxResult getInfo(@PathVariable("jobId") Long jobId)
    {
        return success(jobInformationService.selectJobInformationByJobId(jobId));
    }

    /**
     * 新增岗位信息
     */
    //@preAuthorize("@ss.hasPermi('system:information:add')")
    @Log(title = "岗位信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JobInformation jobInformation)
    {
        return toAjax(jobInformationService.insertJobInformation(jobInformation));
    }

    /**
     * 修改岗位信息
     */
    //@preAuthorize("@ss.hasPermi('system:information:edit')")
    @Log(title = "岗位信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JobInformation jobInformation)
    {
//        System.out.println(jobInformation);
        return toAjax(jobInformationService.updateJobInformation(jobInformation));
    }

    /**
     * 删除岗位信息
     */
    //@preAuthorize("@ss.hasPermi('system:information:remove')")
    @Log(title = "岗位信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{jobIds}")
    public AjaxResult remove(@PathVariable Integer[] jobIds)
    {

        return toAjax(jobInformationService.deleteJobInformationByJobIds(jobIds));
    }

    @GetMapping("/canDo/{jobId}")
    public TableDataInfo canDoList(@PathVariable("jobId") Integer jobId){
        startPage();
        CanDo canDo = new CanDo();
        canDo.setJobId(jobId);
        List<CanDo> canDos = jobInformationService.selectCanDo(canDo);
        return getDataTable(canDos);
    }

    @GetMapping("/canDo/one/{id}")
    public AjaxResult getCanDo(@PathVariable("id") Long id){
        CanDo canDo = new CanDo();
        canDo.setId(id);
        List<CanDo> canDos = jobInformationService.selectCanDo(canDo);
        if (canDos.size()==1){
            return AjaxResult.success(canDos.get(0));
        }
        return AjaxResult.error();
    }

    @PutMapping("/canDo")
    public AjaxResult updateCanDo(@RequestBody CanDo canDo){
        jobInformationService.updateCanDo(canDo);
        return AjaxResult.success();
    }
    @PostMapping("/canDo")
    public AjaxResult addCanDo(@RequestBody CanDo canDo){
        jobInformationService.insertCanDo(canDo);
        return AjaxResult.success();
    }

    @DeleteMapping("/canDo/{id}")
    public AjaxResult deleteCanDo(@PathVariable("id")Long id){
        jobInformationService.deleteCanDo(id);
        return AjaxResult.success();
    }

}

